package com.timous.web.admin;


import com.timous.Entity.Blog;
import com.timous.Entity.User;
import com.timous.service.BlogService;
import com.timous.service.TagService;
import com.timous.service.TypeService;
import com.timous.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String INPUT = "admin/blogpublished";
    private static final String UPDATE = "admin/blogupdate";
    private static final String LIST = "admin/blogmanage";
    private static final String REDIRECT_LIST = "redirect:/admin/blogmanage";

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/blogmanage")
    public String list(@PageableDefault(size = 2, sort = {"updateTime"},
            direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blogQuery, Model model) {

        //初始化分类的信息
        model.addAttribute("types", typeService.listType());

        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
        return LIST;
    }


    //局部的渲染
    @PostMapping("/blogmanage/search")
    public String search(@PageableDefault(size = 2, sort = {"updateTime"},
            direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blogQuery, Model model) {
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
        return "admin/blogmanage :: blogList";
    }


    //新增页面
    @GetMapping("/blogs/blogpublished")
    public String input(Model model) {
        setTypeAndTag(model);
        model.addAttribute("blog", new Blog());
        return INPUT;
    }

    private void setTypeAndTag(Model model) {
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("types", typeService.listType());
    }


    //修改页面
    @GetMapping("/blogs/{id}/blogupdate")
    public String editInput(@PathVariable Long id, Model model) {
        setTypeAndTag(model);
        Blog blog = blogService.getBlog(id);
        model.addAttribute("description", blog.getDescription());
        blog.init();
        model.addAttribute("blog", blog);
        return UPDATE;
    }

    //提交发布的内容,新增和修改
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {

        blog.setUser((User) session.getAttribute("user"));

        //对象属性的初始化
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));

        //对象的标签的初始化
        //需要根据字符串来进行tag的查找，需要到service里面进行查找
        Blog b;//存储博客
        //根据是否传递过来id进行判断
        if (blog.getId() == null) {
            b = blogService.saveBlog(blog);
        } else {
            b = blogService.updateBlog(blog.getId(), blog);
        }
        blog.setTags(tagService.listTag(blog.getTagIds()));
        //


        if (b == null) {
            attributes.addFlashAttribute("message", "新增失败！");
        } else {
            attributes.addFlashAttribute("message", "新增成功！");
        }
        return REDIRECT_LIST;
    }

    @PostMapping("/blogs/update")
    public String update(Blog blog, RedirectAttributes attributes, HttpSession session) {

        blog.setUser((User) session.getAttribute("user"));

        //对象属性的初始化
        blog.setType(typeService.getType(blog.getType().getId()));

        //对象的标签的初始化
        //需要根据字符串来进行tag的查找，需要到service里面进行查找
        blog.setTags(tagService.listTag(blog.getTagIds()));
        //
        Blog b = blogService.saveBlog(blog);//存储博客

        if (b == null) {
            attributes.addFlashAttribute("message", "修改失败！");
        } else {
            attributes.addFlashAttribute("message", "修改成功！");
        }
        return REDIRECT_LIST;
    }

    @RequestMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return REDIRECT_LIST;
    }

}
